import {observable, action,toJS} from 'mobx';
import { SearchAdvanced } from 'weaver-mobile-page';
import * as Api from '../../api/MyBlogApi';
import * as Count from '../../api/UserCenterApi'
import { PARAMS ,renderNoData} from '../../components/public/BlogUtils';
import BlogCommentStore from '../comment/BlogCommentStore';

const initalState = {
    currentPage: 1,
    blogPage: 1,
    photoSwipePage: 1,
    blogEndDate: '',
    commentCount: 0,
    BlogCommentStoreMap: {},
    isSearch: false,
    searchAdValue: '',
}

export default class MyBlogStore {

@observable currentPage = initalState.currentPage;
@observable photoSwipePage = initalState.photoSwipePage;//翻页图片预览
@observable blogEndDate = initalState.blogEndDate;
@observable commentCount = initalState.commentCount;
@observable myBlog = new BlogCommentStore({ getComment: this.getMyBlogList });
@observable comment = new BlogCommentStore({ getComment: this.getCommentList });
@observable laud = new BlogCommentStore({ getComment: this.getLaudList });
@observable atMe = new BlogCommentStore({ getComment: this.getAtMeList });
@observable BlogCommentStoreMap = initalState.BlogCommentStoreMap;

@observable isSearch = initalState.isSearch;
@observable searchAdValue = initalState.searchAdValue; // 搜索值
@observable search = new SearchAdvanced.Store({ route: '/myBlog/blog', id:'myBlogAdsearch' });  // 高级搜索
@observable blogPage = initalState.blogPage;

@action setStoreState = (params) => {
    Object.keys(params).map(key => {
    this[key] = params[key];
})
}

// 请求评论，赞 数字
getMenuCountData = () => {

    Count.getMenuCountData().then(data=> {
        this.commentCount = data.countData.commentCount;
})
}

/* 我的微博--微博 */
// 获得高级搜索数据
blogSearchCondition = () => {
    this.search.getData({
        url:`/api/blog/base/blogSearchCondition`,
        method: `GET`,
        params: {...PARAMS, listType:'myblog'}
})
}

  @action('请求我的微博-微博数据')
getMyBlogList = () => {
    if(this.blogEndDate==""){

        if(this.myBlog.blogCommentStore.replyList.length>0){
            this.blogEndDate=this.myBlog.blogCommentStore.replyList.pop().workdate;
        }

    }

    if (this.isSearch||this.search.form.formParams.content !== '') {
        let searchParams = this.search.form.formParams || {};
        let params = {
                pageSize: 10,
                currentPage: this.blogPage++,
                ...searchParams,
            }
        return Api.getMyBlogListBySearch(params).then(data => {
            const { status, replyList, currentPage, total,pageSize,endWorkdate } = data;
            this.blogEndDate=endWorkdate;
        if(status === '1' && total < currentPage * pageSize) {
            let data_params={};
            if(currentPage==1){
                total>replyList.length;
                data_params  = {hasMore: true}
            }else{
                data_params  = {hasMore: false}
            }
            Object.assign(data, data_params)
        }
        this.photoSwipePage++;
        return data
    })
    } else {
        let params = {
            endDate: this.blogEndDate,
            pageSize: 10,
        }
        return Api.getMyBlogList(params).then(data => {
        if(data.status === '1'&& data.replyList.length>0) {
            this.blogEndDate=data.endWorkdate;
        }else {
            let data_params = {hasMore: false}
            Object.assign(data, data_params)
        }
        this.photoSwipePage++;
        return data;
    })
    }
}

  @action('关联高级搜索数据')
updateAdSearchSubject = (value) => {
    this.searchAdValue = value;
    this.search.form.updateField('content', value);
}

  @action('搜索')
searchBlog = () => {
    this.isSearch = true;
    this.blogPage = 1;
    this.myBlog.blogCommentStore.getDatas();
}

/* 我的微博--评论 */
@action('请求我的微博-评论数据')
getCommentList = () => {
    let params = {
        type: 'reply',
        currentPage: this.currentPage++,
        pageSize: 10,
    }
    return Api.getMyblogListByType(params).then(data => {
        const { status, total, currentPage, pageSize } = data;

    if(status === '1' && total < currentPage * pageSize) {
        let data_params = {hasMore: false}
        Object.assign(data, data_params)
    }
    this.photoSwipePage++;
    return data
})
}

/* 我的微博--赞 */
@action('请求我的微博-赞数据')
getLaudList = () => {
    let params = {
        type: 'zan',
        currentPage: this.currentPage++,
        pageSize: 10,
    }
    return Api.getMyblogListByType(params).then(data => {
        const { status, total, currentPage, pageSize } = data;
    if(status === '1' && total < currentPage * pageSize) {
        let data_params = {hasMore: false}
        Object.assign(data, data_params)
    }
    this.photoSwipePage++;
    return data
})
}

/* 我的微博--@我 */
@action('请求我的微博-@我数据')
getAtMeList = () => {
    let params = {
        type: 'at',
        currentPage: this.currentPage++,
        pageSize: 10,
    }
    return Api.getMyblogListByType(params).then(data => {
        const { status, total, currentPage, pageSize } = data;
    if(status === '1' && total < currentPage * pageSize) {
        let data_params = {hasMore: false}
        Object.assign(data, data_params)
    }
    this.photoSwipePage++;
    return data
})
}

setCommentCount = () => {
    this.commentCount = 0;
}

// 初始化数据
initCondition = () => {
    this.setStoreState(initalState);
}
}